package com.hzya.frame.web.tag;
import com.hzya.frame.system.module.entity.ModuleEntity;
import com.hzya.frame.system.user.entity.UserEntity;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 菜单自定义标签
 * @author Administrator
 *
 */
public class MenuTag extends TagSupport {


	private HttpServletRequest httpServletRequest;

	private HttpServletResponse httpServletRespons;

	private List<ModuleEntity> usersModuleList = Collections.EMPTY_LIST;

	private String menuId;

	private ModuleEntity upModule;

	private UserEntity getCurrentUser() {
		HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
		return (UserEntity) request.getSession().getAttribute("_CURRENT_USER");
	}

	public void init() {
		httpServletRespons = (HttpServletResponse) this.pageContext.getResponse();
		httpServletRequest = (HttpServletRequest) this.pageContext.getRequest();
		ServletContext servletContext = this.pageContext.getServletContext();
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
		//systemModuleDao = (IModuleDao) wac.getBean("systemModuleDao");
		usersModuleList = getCurrentUser().getModulesList();
	}

	/**
	 * 是否有菜单权限
	 * 
	 * @param targetModule
	 * @return
	 */
	public boolean haisPermission(ModuleEntity targetModule) {
		boolean flag = false;
		flag = usersModuleList.contains(targetModule);
		return flag;
	}

	@Override
	public int doEndTag() throws JspException {
		init();
		if (null != usersModuleList && 0 < usersModuleList.size() && upModule != null) {
			StringBuffer tag = new StringBuffer();
			ModuleEntity paramDomain = new ModuleEntity();
			UserEntity systemUserDomain = getCurrentUser();
			int count = 1;

			List<ModuleEntity> menuList = Collections.EMPTY_LIST;

			if (haisPermission(upModule)) {
				menuList = filteModules(upModule);
				for (ModuleEntity tempDomain : menuList) {
					List<ModuleEntity> subLists = filteModules(tempDomain);
					if (null != subLists && 0 < subLists.size()) {
						tag.append("<li>"
								+ "<a class=\"title\" ismenu='true' href=\"#Menu=ChildMenu" + count + "\" id=\"menu_" + count + "\""
								+ " onClick=\"DoMenu('ChildMenu" + count+ "');"
								+ "showContent(this.id," + count + ",'menu_','tittlebg','title');"
								+ "appendSecondNavegate('" + tempDomain.getName() + "','ChildMenu" + count + "',this.id," + count+ ",'menu_')\">"
								+ " <img src=\"" + httpServletRequest.getContextPath() + "/common/images/base.gif\"   align=\'absmiddle\' border=\"0\"> "
								+ "<span>" + tempDomain.getName()+ "</span></a>\n");
						tag.append("<ul id=\"ChildMenu" + count + "\" class=\"collapsed\">\n");
						for (ModuleEntity childModule : subLists) {
							tag.append("<li>\n");
							String url = null;
							if (!"".equals(childModule.getUrl())) {
								url = childModule.getUrl();
								if ("/".equals(url.substring(0, 1))) {
									url = url.substring(1);
								}
								if (url.indexOf("tabs") >= 0) {
									url += "/" + childModule.getId();
								}
							}

							tag.append("<a  onclick=\""
												+ "goMainnr('mainnr','" + httpServletRequest.getContextPath() + "/" + url + "');"
												+ "appendThirdNavegate('mainnr','"+ httpServletRequest.getContextPath() + "/" + url + "','" + childModule.getName() + "');\" "
												+ " href=\"#\" >" + childModule.getName() + ""
												+ "</a>");
							tag.append("</li>\n");
						}
					} else {
						tag.append("<li>"
								+ "<a class=\"title\" ismenu='true' href=\"" + httpServletRequest.getContextPath() + "/" + tempDomain.getUrl() + "\""
								+ " onClick=\"appendSecondNavegate('" + tempDomain.getName() + "','ChildMenu" + count + "',this.id," + count+ ",'menu_')\" "
								+ " target=\"mainnr\" id=\"menu_" + count+ "\"> "
								+ " <img src=\"" + httpServletRequest.getContextPath() + "/common/images/base.gif\"   align=\'absmiddle\' border=\"0\">"
								+ " <span>" + tempDomain.getName()+ "</span></a>\n");
						tag.append("<ul id=\"ChildMenu" + count + "\" class=\"collapsed\">\n");
					}
					tag.append("</ul>\n");
					tag.append("</li>\n");
					count++;
				}

			}
			JspWriter out = this.pageContext.getOut();
			try {
				out.print(tag);
				out.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return super.doEndTag();
	}

	/**
	 * 筛选用户模块
	 * 
	 * @param upDomain
	 * @return
	 */
	private List<ModuleEntity> filteModules(ModuleEntity upDomain) {
		List<ModuleEntity> resultList = Collections.EMPTY_LIST;
		if (upDomain != null && usersModuleList != null) {
			Long up_id = upDomain.getId();
			resultList = new ArrayList<ModuleEntity>();
			for (ModuleEntity tempDomain : usersModuleList) {
				if (up_id.equals(tempDomain.getUp_id()) && "menu".equals(tempDomain.getType())  ) {
					resultList.add(tempDomain);
					Collections.sort(resultList);
				}

			}

		}

		return resultList;
	}

	@Override
	public int doAfterBody() throws JspException {
		// TODO Auto-generated method stub

		return super.doAfterBody();

	}

	@Override
	public int doStartTag() throws JspException {
		return EVAL_PAGE;
	}

	public String getMenuId() {
		return menuId;
	}

	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}

	public ModuleEntity getUpModule() {
		return upModule;
	}

	public void setUpModule(ModuleEntity upModule) {
		this.upModule = upModule;
	}

}
